<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodsInfo">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goodsInfo.categories[1].id">{{
          goodsInfo.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/' + goodsInfo.categories[0].id">{{
          goodsInfo.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 图片组件 -->
          <GoodsImage :goods="goodsInfo.mainPictures"></GoodsImage>
          <!-- 销售信息 -->
          <GoodsSales :goods="goodsInfo"></GoodsSales>
        </div>
        <div class="spec">
          <GoodName :goods="goodsInfo"></GoodName>
          <!-- 商品spu -->
          <GoodsSku :goods="goodsInfo" @getSkuId="getSkuId"></GoodsSku>
          <!-- 数量选择 
          1:父子组件v-model传值
          2:加减按钮功能实现
          3:最大值与最小值限制
          4:标题传值控制          
          -->
          <XtxNumbox :min="1" :max="10" v-model="goodsNum"></XtxNumbox>
          <!-- 按钮 -->
          <XtxButton size="large" type="primary" style="margin-top:15px;"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 
      1:完成基本组件html
      2:定义api
      3:调用api,获取数据
      4：将数据转换成符合轮播图格式数据（调整html）进行渲染
      5：样式小调整      
      -->
      <GoodsRelevant :id="goodsInfo.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 
          分页：
          1：创建分页组件
          2：完成html基本结构
          3：用于评论组件上
          4：实现基本逻辑（上一页，下一页，接收传参（page,pagesize...））
             page:页码
             pagesize:页容量
             total:总数
          5:完善相应的中间分页功能          
          
          -->
          <div class="goods-tabs">
            <GoodsTabs></GoodsTabs>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goodsWarn></goodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :id="goodsInfo.id" :type="1"></GoodsHot>
          <GoodsHot :id="goodsInfo.id" :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import { goods } from '@/api/product'
import { useRoute } from 'vue-router'
import { ref, watch, provide } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import goodsWarn from './components/goods-warn.vue'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodName,
    GoodsSku,
    GoodsRelevant,
    GoodsTabs,
    GoodsHot,
    goodsWarn
  },
  setup () {
    const route = useRoute()
    const goodsInfo = ref('')
    provide('goods', goodsInfo)
    watch(
      () => route.params.id,
      () => {
        goodsInfo.value = ''
        goods(route.params.id).then(res => {
          goodsInfo.value = res.result
          console.log('商品详情:', res)
        })
      },
      {
        immediate: true
      }
    )

    function getSkuId (obj) {
      console.log(obj)
    }
    // 商品数量
    const goodsNum = ref(1)
    return { goodsInfo, getSkuId, goodsNum }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
